package com.zzh.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2021-06-01
 * @desc
 */
public class VerticalViewPager extends ViewPager {

    public VerticalViewPager(@NonNull Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {


    }

    public VerticalViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        if (views.get(getCurrentItem()) instanceof ScrollView) {//getChild(getCurrentItem)获取的item不正确，不要用
//            ScrollView scrollView = (ScrollView) views.get(getCurrentItem());
//            switch (ev.getAction()) {
//                case ACTION_DOWN:
//                    fingerDownPosition = ev.getY(0);
//                    break;
//                case ACTION_MOVE:
//                    if ((ev.getY() - fingerDownPosition) > 0) {
//                        if (scrollView.canScrollVertically(-1)) {
//                            return false;
//                        } else {
//                            return true;
//                        }
//                    } else if (ev.getY() - fingerDownPosition < 0){
//                        if (scrollView.canScrollVertically(1)){
//                            return false;
//                        } else
//                            return true;
//                    }
//            }
//
//        }
        boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
//        swapXY(ev); // return touch coordinates to original reference frame for any child views
        return intercepted;
    }


    /**
     * 交换x 和 y 值
     *
     * @param event 事件类
     * @return 处理过的事件类
     */
    private MotionEvent swapXY(MotionEvent event) {
        float width = getWidth();
        float height = getHeight();
        float newX = (event.getY() / height) * width;
        float newY = (event.getX() / width) * height;
        event.setLocation(newX, newY);
        return event;
    }

}
